/****************************************************************************

	Copyright(c) 2009, Ravindra Bawaskar
	All rights reserved.

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

***************************************************************************/

$(document).ready(function() {
	if(processConfig()) {
		addValidate();
	}
});

function processConfig() {
	var ret = false;
	if(typeof validation !== "undefined") {
		ret = true;
		for(var i=0; i < validation.length; i++) {
			var elemId = validation[i].elem;
			var elemValues = validation[i].values;
			for(var j = 0; j < elemValues.length; j++) {
				var attrName = elemValues[j][0];
				var attrValue = elemValues[j][1];
						
				$(elemId).attr(attrName, attrValue);
			}
		}
	}
	return ret;
}

function addValidate() {
	var forms = document.getElementsByTagName("form");
	for( var i=0; i<forms.length; i++)
	{
		forms[i].onsubmit = validateForm;
	}
};

function validateForm() 
{
	var obj = this;
	var returnFlag = true;
	for (var formElementsCount=0;formElementsCount<obj.elements.length; formElementsCount++)
	{
		var formElement = obj.elements[formElementsCount];
		if (formElement.getAttribute('v')) {
			var method = formElement.getAttribute('v');
			var func = method+'("'+formElement.id+'")';
			var response = eval("("+func+")");
			if(response!==true)
			{
				returnFlag = false;
				showError(formElement, response);
			}
			else
			{
				hideError(formElement);
			}
		}
	}
	return returnFlag;
};

function email(id)
{
	var errorMessage = getErrorMessage(id);
	if(!errorMessage)
		errorMessage = "invalid email address";
	
	var str = document.getElementById(id).value;
	if (str.length==0)
		return errorMessage;
	var filter = /^((([a-z]|[A-Z]|[0-9]|\-|_)+(\.([a-z]|[A-Z]|[0-9]|\-|_)+)*)@((((([a-z]|[A-Z]|[0-9])([a-z]|[A-Z]|[0-9]|\-){0,61}([a-z]|[A-Z]|[0-9])\.))*([a-z]|[A-Z]|[0-9])([a-z]|[A-Z]|[0-9]|\-){0,61}([a-z]|[A-Z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/;
	if (!filter.test(str))
		return errorMessage;

	return true;
}

function checked(id)
{
	var errorMessage = getErrorMessage(id);
	if(!errorMessage)
		errorMessage = "invalid value";
		
	var ele = document.getElementById(id);
	if(!$(ele).is(":checked")){
		return errorMessage;
	}
	
	return true;
}

function regex(id)
{
	var errorMessage = getErrorMessage(id);
	if(!errorMessage)
		errorMessage = "invalid value";
	
	var ele = document.getElementById(id);
	var pt =  ele.getAttribute("pt") == null ? "" : ele.getAttribute("pt");
	var re = new RegExp(pt);
	var str = document.getElementById(id).value;
	
	if (!re.test(str))
		return errorMessage;

	return true;
}

function str(id)
{
	var errorMessage = getErrorMessage(id);
	if(!errorMessage)
		errorMessage = "invalid value";
		
	var ele = document.getElementById(id);
	// var c = ele.getAttribute("c") == null ? "" : ele.getAttribute("c");
	if(ele.value.length == 0)
		return errorMessage;
	var min =  ele.getAttribute("mi") == null ? "" : ele.getAttribute("mi");
	if(min!="" && ele.value.length < min)
		return errorMessage;
	var max = ele.getAttribute("mx") == null ? "" : ele.getAttribute("mx");
	if(max!="" &&  ele.value.length > max)
		return errorMessage;
	return true;
}

function int(id)
{
	var errorMessage = getErrorMessage(id);
	if(!errorMessage)
		errorMessage = "invalid value";
		
	var ele = document.getElementById(id);
	if (isNaN (ele.value))
		return errorMessage;
	return str(id);
}

function file(id)
{
	var errorMessage = getErrorMessage(id);
	if(!errorMessage)
		errorMessage = "select file to upload";
		
	var ele = document.getElementById(id);
	if (ele.value.length == 0)
		return errorMessage;
	return true;
}
function showError(ele, response)
{
	var valErrorElement = $(ele).siblings(".validation_error");
	$(valErrorElement).html(response);
	
	// hideError(ele);
	// var errorspan = document.createElement("DIV");
	// errorspan.innerHTML = response;
	// errorspan.className = "validation_error"; 
	// appendAfter(ele, errorspan);
}

function hideError(ele)
{
	
	var valErrorElement = $(ele).siblings(".validation_error");
	$(valErrorElement).html("");
	
	// var errorspan = ele.nextSibling;
	// if (!errorspan)
	// 	return;
	// if (errorspan.className = "error")
	// 	ele.parentNode.removeChild(errorspan);
}

function appendAfter(obj,node) 
{
	if (obj.nextSibling)
		obj.parentNode.insertBefore(node,obj.nextSibling);
	else
		obj.parentNode.appendChild(node);
}

function getErrorMessage(id)
{
	var ele = document.getElementById(id);
	var errorMessage =  ele.getAttribute("msg") == null ? false : ele.getAttribute("msg");
	return errorMessage;
}
